package org.newcih.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;

public class HelloWorld {

    static ProcessEngine engine;

    static {
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration
                .createProcessEngineConfigurationFromResource("applicationContext.xml", "processEngineConfiguration");
        engine = configuration.buildProcessEngine();
    }

    @Test
    public void test1() {
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        configuration.setJdbcDriver("com.mysql.jdbc.Driver");
        configuration.setJdbcUrl("jdbc:mysql://localhost:3306/sample");
        configuration.setJdbcUsername("root");
        configuration.setJdbcPassword("newcih");
        configuration.setDatabaseSchemaUpdate("true");

        ProcessEngine engine = configuration.buildProcessEngine();
    }

    @Test
    public void test2() {
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration
                .createProcessEngineConfigurationFromResource("applicationContext.xml", "processEngineConfiguration");
        ProcessEngine engine = configuration.buildProcessEngine();
    }

    @Test
    public void test3() {
//        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
//        System.out.println(engine);
    }

    @Test
    public void test4() {

        DeploymentBuilder builder = engine.getRepositoryService().createDeployment();
        builder.addClasspathResource("activiti/MyProcess.bpmn");

        Deployment deployment = builder.deploy();

        System.out.println(deployment.getId());

        ProcessInstance instance = engine.getRuntimeService().startProcessInstanceByKey("ExclusiveGateway");

        System.out.println(instance.getId());
        System.out.println(instance.getProcessDefinitionId());
    }

    @Test
    public void test5() {
        ProcessDefinitionQuery query = engine.getRepositoryService().createProcessDefinitionQuery();
//        List<ProcessDefinition> list = query.list();
        query.processDefinitionKey("myProcess_1").list().forEach(process ->
                System.out.println(process.getId() + "  " + process.getName() + " " + process.getVersion())
        );
//        list.forEach(process ->
//                System.out.println(process.getId() + "  " + process.getName() + " " + process.getVersion())
//        );
    }

    @Test
    public void test6() {
        String processDefinitionId = "myProcess_1:1:2504";

        ProcessInstance instance = engine.getRuntimeService().startProcessInstanceById(processDefinitionId);

        System.out.println(instance.getId());
    }

    @Test
    public void test7() {
        TaskQuery query = engine.getTaskService().createTaskQuery();

        query.taskAssignee("newcih");

        query.list().forEach(task -> System.out.println(task.getId() + "\t" + task.getName() + "\t" + task.getAssignee()));
    }

    @Test
    public void test8() {
        String taskId = "304";
        engine.getTaskService().complete(taskId);
    }
}
